<template>
  <div class="app-container" style="width: 1000px">
      权限添加
      <el-form label-width="120px" ref="permissionSet":model="permissionSet">
        <el-form-item label="ID"
                      prop="userId"
                      :rules="[
      { required: true, message: 'id不能为空',trigger: 'blur'},
      { type:'number', message: '请输入正确的id',trigger: 'blur'},
      ]"
        >
          <el-input v-model.number="permissionSet.userId"/>
        </el-form-item>
         <el-form-item label="当前用户"
                       prop="username"
                       :rules="[
      { required: true, message: '用户名不能为空',trigger: 'blur'},
      ]"
         >
            <el-input v-model="permissionSet.username"/>
         </el-form-item>

         <el-form-item label="角色"
                       prop="roles"
                       :rules="[
      { required: true, message: '角色名不能为空',trigger: 'blur'},
      ]"
         >
            <el-input v-model="permissionSet.roles"/>
         </el-form-item>
        <el-form-item label="api名称"

                      prop="permName"
                      :rules="[
      { required: true, message: 'api名不能为空',trigger: 'blur'},
      ]">
          <el-input v-model="permissionSet.permName"/>
        </el-form-item>
        <el-form-item label="api标签"
                      prop="permTag"
                      :rules="[
      { required: true, message: 'tag不能为空',trigger: 'blur'},
      ]">
          <el-input v-model="permissionSet.permTag"/>
        </el-form-item>
         <el-form-item label="api基础路径"
                       prop="permUrl"
                       :rules="[
      { required: true, message: 'url不能为空',trigger: 'blur'},
      ]"
         >
            <el-input v-model="permissionSet.permUrl"/>
         </el-form-item>
         <el-form-item>
            <el-button type="primary" @click="saveOrUpdate('permissionSet')">添加</el-button>
         </el-form-item>
      </el-form>

   </div>
</template>
<script>
import jwtDecode from "jwt-decode";
import {getToken, removeToken} from "@/utils/auth";
import popMessage from "@/utils/popMessage";
import {uploadPermission} from "@/views/api/myselfManager";
import {updateUserBasicApi} from "@/views2/api/myselfManager";
import {MessageBox} from "element-ui";
import Vue from "vue";

export default {
   data() {
      return {
         permissionSet:{
           userId:jwtDecode(getToken()).userId,
           username:jwtDecode(getToken()).username,
           roles:jwtDecode(getToken()).roles,
           permName:'',
           permTag:'',
           permUrl:''
         }
      }
   },
   created() {
   },
   methods: {
     saveOrUpdate(formName)
     {


       /*验证*/
       this.$refs[formName].validate((valid) => {
         if (valid) {

           uploadPermission(this.permissionSet).then(res=>
           {

             if (this.permissionSet.userId===jwtDecode(getToken()).userId)
             {
               MessageBox.confirm(
                   '权限添加成功！请重新登陆',
                   'Tips',
                   {
                     confirmButtonText: '确定',
                     cancelButtonText: '取消',
                     type: 'success'
                   }
               ).then(() => {
                 //好家伙，通信贼好用！！！
                 removeToken();
                 window.location.reload();
               }).catch(()=>{removeToken();
                 window.location.reload();})
             }else {

               popMessage(false,"权限添加成功","success")
             }


           }).catch(err=>{popMessage(false,res.data.message),"error"})

         } else {
           console.log('error submit!!');
           return false;
         }
       });



     }

   }
}
</script>
